$(function () {
    var clienttest_tab = $('#clienttest_tab');
    var clienttest_edit = $("#clienttest_edit");
    var clienttest_dialog = $("#clienttest_dialog");
    var clienttest_roles = $("#clienttest_roles");
    var clienttest_grade_dialog = $("#clienttest_grade_dialog");
    var clienttest_grade_edit = $("#clienttest_grade_edit");
    clienttest_tab.datagrid({
        url:'/clienttest/list',
        fit:true,
        fitColumns:true,
        border:false,
        rownumbers:true,
        pagination:true,
        singleSelect:true,
        toolbar:'#clienttest_tab_tool',
        columns:[[
            {field:'id',title:'考试编号', width: 50,align: 'center'},
            {field:'client',title:'姓名',align: 'center', width: 50,formatter:function (value,row,index) {
                return value ? value.name:"";
            }},
            {field:'client.inputUser',title:'营销人员',align: 'center', width: 50,formatter:function (value,row,index) {
                if(row.client.inputUser){
                    return row.client.inputUser.realName;
                }
                return "";
            }},
            {field:'client.qq',title:'QQ',align: 'center', width: 100,formatter:function (value,row,index) {
                return row.client.qq ? row.client.qq:"";
            }},
            {field:'client.tle',title:'电话',align: 'center', width: 100,formatter:function (value,row,index) {
                return row.client.tel ? row.client.tel:"";
            }},
            {field:'client.wishClass',title:'意向班级',align: 'center', width: 100,formatter:function (value,row,index) {
                return row.client.wishClass ? row.client.wishClass.name:"";
            }},
            {field:'testTime',title:'考试时间', width: 120, align: 'center'},
            {field:'testType',title:'考试类型',align: 'center', width: 100,formatter:function (value,row,index) {
                return value ? value.name:"";
            }},
            {field:'testGrade',title:'考试成绩', width: 50,align: 'center'},
            {field:'testState',title:'考试结果', width: 50,align: 'center',formatter:function (value,row,index) {
                if (value == 0){
                    return "<span style='color: red'>未通过</span>";
                }
                if (value == 1){
                    return "<span style='color: green'>通过</span>";
                }
                return "未考试";
            }},
            {field:'remark',title:'备注',align: 'center', width: 140},
            {field:'handler',title:'处理人', width: 100,align: 'center',formatter:function (value,row,index) {
                return value ? value.realName:"";
            }}
        ]]
    });


    var objMethod = {
        //刷新页面
        reloadList:function () {
            clienttest_tab.datagrid("reload");
        },
        save:function () {
            var url = "/clienttest/save";
            //获取id
            var clienttestId = $("#clienttest_id").val();
            if (clienttestId){
                url = "/clienttest/update"
            }
            //提交表单
            clienttest_edit.form("submit",{
                url:url,
                //表单提交之前

                success:function (data) {
                    data = $.parseJSON(data);
                    if(data.success){
                        $.messager.alert("提示","操作成功","info",function () {
                            //关闭窗口刷新页面
                            objMethod.cancel();
                            objMethod.reloadList();
                        });
                    }else {
                        $.messager.alert("提示",data.msg,"error");
                    }
                }
            });
        },
        //关闭窗口
        cancel:function () {
            clienttest_dialog.dialog("close");
            clienttest_grade_dialog.dialog("close");
        },
        //打开编辑窗口
        addclienttest:function () {
            //清空表中数据
            clienttest_edit.form("clear");
            clienttest_dialog.dialog("setTitle","编辑");
            clienttest_dialog.dialog("open");
        },



        //编辑成绩
        editGrade:function () {
            //获取选中的行
            var selectedRow = clienttest_tab.datagrid("getSelected");
            //判断是否选中行
            if(!selectedRow){
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            //清空表中数据
            clienttest_grade_edit.form("clear");
            clienttest_grade_edit.form("load",selectedRow);
            clienttest_grade_dialog.dialog("open");
        },
        //编辑成绩 保存
        savetest:function () {
            var url = "/clienttest/savegrade";
            //提交表单
            clienttest_grade_edit.form("submit",{
                url:url,
                //表单提交之前
                success:function (data) {
                    data = $.parseJSON(data);
                    if(data.success){
                        $.messager.alert("提示","操作成功","info",function () {
                            //关闭窗口刷新页面
                            objMethod.cancel();
                            objMethod.reloadList();
                        });
                    }else {
                        $.messager.alert("提示",data.msg,"error");
                    }
                }
            });
        },


        //员工离职
        deleteclienttest:function () {
            //获取选中的行
            var selectedRow = clienttest_tab.datagrid("getSelected");
            //判断是否选中行
            if(!selectedRow){
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            $.messager.confirm("提示","是否确定删除该条数据",function (yes) {
               if (yes){
                   $.post("/clienttest/delete",{id:selectedRow.id},function (data) {
                      if(data.success){
                          $.messager.alert("提示","操作成功","info",function () {
                              //刷新页面
                              objMethod.reloadList();
                          });
                      } else {
                          $.messager.alert("提示",data.msg,"error");
                      }
                   });
               }
            });
        },
        //编辑
        editclienttest:function () {
            //获取选中的行
            var selectedRow = clienttest_tab.datagrid("getSelected");
            //判断是否选中行
            if(!selectedRow){
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            //设置表单回显--清空表中数据
            clienttest_edit.form("clear");

            //设置关联对象回显---
            if(selectedRow.testType){
                selectedRow['testType.id'] = selectedRow.testType.id;
            }
            clienttest_edit.form("load",selectedRow);
            clienttest_dialog.dialog("open");
        },

        //高级查询
        queryForList:function () {
            var keyword = $("#keyword").val();
            clienttest_tab.datagrid("load",{keyword:keyword});
        },
        downloadList:function () {
            window.location.href="/clienttest/download";
        }

    };
    //统一绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        objMethod[cmd]();
    });
    //新增弹框
    clienttest_dialog.dialog({
        title: '添加学员考试',
        closed: true,
        buttons:'#clienttest_edit_tool'
    });
    //考试成绩弹框
    clienttest_grade_dialog.dialog({
        title: '学员考试成绩',
        closed: true,
        buttons:'#test_grade_edit_tool'
    });
});
